﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace MD5
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
		
		private void alert(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void Button_Hash_Click(object sender, EventArgs e)
        {
            if (FileUpload_Plik.PostedFile.FileName == "")
                alert("Załaduj plik.");
            else
            {
                string wy = Server.MapPath(FileUpload_Plik.PostedFile.FileName) +"_md5.txt";
                StreamWriter sw = new StreamWriter(File.Open(wy, FileMode.Create));
                String filename = Server.MapPath(FileUpload_Plik.PostedFile.FileName);

                MD5 md5 = new MD5();
                byte[] hash = md5.ComputeHash(new FileStream(filename, FileMode.Open));
                foreach (var b in hash)
                    sw.Write(b.ToString("x").PadLeft(2, '0'));
                sw.Close();
                alert("Hashowanie zakończone. Zapisano jako " + FileUpload_Plik.PostedFile.FileName + "_md5.txt");
            }
        }
    }
}
